package com.example.shopping_web_back.controller;


import com.example.shopping_web_back.pojo.BrowsingHistory;
import com.example.shopping_web_back.pojo.PageBean;
import com.example.shopping_web_back.pojo.Result;
import com.example.shopping_web_back.service.impl.BrowsingHistoryServiceImpl;
import com.example.shopping_web_back.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/browsingHistory")
@Validated
public class BrowsingHistoryController {

    @Autowired
    private BrowsingHistoryServiceImpl browsingHistoryServiceImpl;

    @GetMapping("/getBrowsingHistory")
    public Result<PageBean<BrowsingHistory>> getBrowsingHistory(
            Integer pageNum, //当前页
            Integer pageSize //每页个数
    ) {
        PageBean<BrowsingHistory> pbBH = browsingHistoryServiceImpl.getBrowsingHistory(pageNum, pageSize);
        return Result.success(pbBH);
    }

    @PostMapping("/addBrowsingHistory")
    public Result addBrowsingHistory(@RequestBody @Validated BrowsingHistory browsingHistory) {
        Map<String, String> map = ThreadLocalUtil.get();
        String customerName = map.get("customer_name");
        browsingHistory.setCustomerName(customerName);
        browsingHistoryServiceImpl.addBrowsingHistory(browsingHistory);
        return Result.success();
    }

    @PostMapping("/deleteBrowsingHistory")
    public Result deleteBrowsingHistory(@RequestBody @Validated BrowsingHistory browsingHistory) {
        browsingHistoryServiceImpl.deleteBrowsingHistory(browsingHistory);
        return Result.success();
    }
}
